package com.xszx.controller;

import com.xszx.entity.RoleMenu;
import com.xszx.entity.TMeneu;
import com.xszx.entity.TRoel;
import com.xszx.entity.TUser;
import com.xszx.service.RoleMenuService;
import com.xszx.service.TMeneuService;
import com.xszx.service.TRoelService;
import com.xszx.util.SnowFlakeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@Controller
public class MeneuController {

    @Autowired
    TMeneuService meneuService;

    @Autowired
    RoleMenuService roleMenuService;

    @Autowired
    TRoelService roelService;

    @ResponseBody
    @RequestMapping("/getMenusByMid")
    public TMeneu getMenusByMid(@RequestParam("mid") long mid){

        TMeneu meneu = meneuService.selectMenuList(mid);
        log.info("根据菜单id查找菜单：{},{}",mid,meneu);
        return meneu;
    }


    @ResponseBody
    @RequestMapping("/insertMenus")
    public int insertMenus(@RequestParam("mname") String mname, @RequestParam("url") String url, HttpServletRequest request){

        TUser user = (TUser) request.getSession().getAttribute("user");

        TRoel role = roelService.getRoelNameByAccount(user.getAccount());

        TMeneu menuByName = meneuService.getMenuByName(mname);

        if(menuByName==null){
            long mid = new SnowFlakeUtil().getNextId();
            TMeneu meneu = new TMeneu();
            meneu.setMid(mid);
            meneu.setMname(mname);
            meneu.setUrl(url);
            log.info("添加菜单和菜单角色中间表：{}",meneu);
            int i = meneuService.insertMenus(meneu);
            roleMenuService.insertRoleMenu(role.getRid(),mid);
            return i;
        }

        return 0;
    }


    /**
     * 遍历所有菜单信息
     * @return
     */
    @RequestMapping("/selectMenus")
    @ResponseBody
    public List<TMeneu> selectList(){
        List<TMeneu> tMeneus = meneuService.selectMenus();
       log.info("遍历所有菜单：{}",tMeneus);
        return tMeneus;
    }

    @RequestMapping("/deleteMenus")
    public String deleteMenus(@RequestParam("mid") long mid){
        log.info("根据mid删除菜单：{}");
        meneuService.deleteMenus(mid);
        roleMenuService.deleteRoleMenu(mid);

        return "jsp/menuTable";
    }

    @RequestMapping("/updateMenus")
    public String updateMenus(TMeneu meneu){
//        2151813821
        log.info("修改菜单：{}",meneu);
        TMeneu menuByName = meneuService.getMenuByName(meneu.getMname());

        if(menuByName==null){
            int i = meneuService.updateMenus(meneu);
            return "jsp/menuTable";
        } else {
            if(menuByName.getMid().equals(meneu.getMid())){
                int i = meneuService.updateMenus(meneu);
                return "jsp/menuTable";
            }
        }

        return "jsp/menuTable";

    }
}
